package com.hp.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hp.mapper.ParaMapper;
import com.hp.mapper.TemplateMapper;
import com.hp.pojo.Para;
import com.hp.pojo.Template;
import com.hp.service.ParaService;
import com.hp.util.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ParaServiceImpl implements ParaService {
    @Resource
    private ParaMapper paraMapper;

    @Resource
    private TemplateMapper templateMapper;

    @Override
    public PageResult findParaPage(int pageNum, int pageSize, Para para) {
        PageHelper.startPage(pageNum,pageSize);
        Page page=(Page)paraMapper.findParaTemplateId(para);
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    @Override
    public void addPara(Para para) {
        paraMapper.addPara(para);
        Template template = templateMapper.findById(para.getTemplateId());
        template.setParaNum(template.getParaNum()+1);
        templateMapper.update(template);
    }

    @Override
    public Para findPara(int id) {
        return paraMapper.findPara(id);
    }

    @Override
    public void updatePara(Para para) {
        paraMapper.updatePara(para);
    }

    @Override
    public void deletePara(int id) {
        /**
         * 执行流程为先减后删除
         * 先删除id就会为空
         */
        //根据参数表id查询template_id
        Para para = paraMapper.findPara(id);
        //根据template_id查询参数数量
        Template template = templateMapper.findById(para.getTemplateId());
        //使参数数量在删除后减一
        template.setParaNum(template.getParaNum()-1);
        templateMapper.update(template);
        paraMapper.deletePara(id);
    }
}
